
package com.epam.video.test;

import com.epam.video.entities.Answer;
import com.epam.video.entities.Ivc;
import com.epam.video.entities.Ivc.Interaction;
import com.epam.video.entities.Lection;
import com.epam.video.entities.Si;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class JsonParserUtils {
  private static final String DURATION = "duration";

  private static final String NEXT_URL = "next_url";

  private static final String URL = "url";

  private static final String INTERACTIONS = "interactions";

  private static final String TIMESTART = "timestart";

  private static final String TIMESTOP = "timestop";

  private static final String TEXT = "text";

  private static final String QUESTION = "question";

  private static final String ANSWERS = "answers";

  private static final String SI = "si";

  private static final String IVC = "ivc";

  private static final String LECTIONS = "lections";

  private static final String NAME = "name";

  private static final String ID = "id";

  private static final String DESCRIPTION = "description";

  private static final String IVCS = "ivcs";

  private static final String STATUS = "status";

  private static final String RIGHT = "right";

  public static Ivc parseIvc(final String ivcInJson) {
    Ivc ivc = new Ivc();
    JSONObject ivcJsonItem;
    try {
      ivcJsonItem = new JSONObject(ivcInJson).getJSONObject(IVC);
      ivc.setUrl(Uri.parse(ivcJsonItem.getString(URL)));
      ivc.setDuration(ivcJsonItem.getInt(DURATION));
      ivc.setNextUrl(ivcJsonItem.getString(NEXT_URL));
      JSONArray interactionsJsonList = ivcJsonItem.getJSONArray(INTERACTIONS);
      List<Interaction> interactionsList = new ArrayList<Ivc.Interaction>();
      for (int j = 0; j < interactionsJsonList.length(); j++) {
        JSONObject interactionJsonItem = interactionsJsonList.getJSONObject(j);
        Interaction interaction = new Interaction();
        interaction.setTimestart(interactionJsonItem.getString(TIMESTART));
        interaction.setTimestop(interactionJsonItem.getString(TIMESTOP));
        interaction.setText(interactionJsonItem.getString(TEXT));
        interactionsList.add(interaction);
      }
      ivc.setInteraction(interactionsList);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return ivc;
  }

  public static Si parseSi(final String siInJson) {
    Si si = new Si();
    try {
      JSONObject siJsonItem = new JSONObject(siInJson).getJSONObject(SI);
      si.setQuestion(siJsonItem.getString(QUESTION));
      JSONArray answersJsonList = siJsonItem.getJSONArray(ANSWERS);
      List<String> answers = new ArrayList<String>();
      for (int j = 0; j < answersJsonList.length(); j++) {
        String answer = answersJsonList.getJSONObject(j).getString("" + j);
        answers.add(answer);
      }
      si.setAnswers(answers);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return si;
  }

  public static List<Lection> parseLectionsList(final String lectionsJson) {
    List<Lection> lections = new ArrayList<Lection>();
    try {
      JSONArray jsonList = new JSONObject(lectionsJson).getJSONArray(LECTIONS);

      for (int i = 0; i < jsonList.length(); i++) {
        JSONObject jsonItem = jsonList.getJSONObject(i);

        Lection lection = new Lection();
        lection.setId(jsonItem.getString(ID));
        lection.setName(jsonItem.getString(NAME));
        lection.setDescription(jsonItem.getString(DESCRIPTION));
        lections.add(lection);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return lections;
  }

  public static List<Ivc> getListOfIvcs(final String ivcJSON) {
    List<Ivc> ivcsList = new ArrayList<Ivc>();
    try {
      JSONArray ivcsJsonList = new JSONObject(ivcJSON).getJSONArray(IVCS);
      for (int i = 0; i < ivcsJsonList.length(); i++) {
        JSONObject ivcJsonItem = ivcsJsonList.getJSONObject(i);
        Ivc ivc = new Ivc();
        ivc.setId(ivcJsonItem.getString(ID));
        ivc.setDuration(ivcJsonItem.getInt(DURATION));
        ivcsList.add(ivc);
      }
    } catch (JSONException e) {

      // TODO: handle it appropriatly
      e.printStackTrace();
    }
    return ivcsList;
  }

  /**
   * Parse answer result and return url to appropriate item (either si or ivc)
   * 
   * @param answerJson
   * @return url to si or ivc
   */
  public static Answer parseAnswer(final String answerJson) {
    Answer answer = new Answer();
    try {
      JSONObject answerJsonObject = new JSONObject(answerJson);
      answer.setNextUrl(answerJsonObject.getString(NEXT_URL));
      answer.setRight(answerJsonObject.getBoolean(STATUS));
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return answer;
  }
}
